package com.friendc.Service;

import com.friendc.PrJo.User;
import com.friendc.PrJo.friendc;
import com.friendc.Util.JdbcUtil;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author 20424
 * @Date 2020/8/4 11:40
 * @Version 1.0
 */
public class UserService {
    //登录方法
    public User login(String username, String password) {
        String sql = "select * from userInfo where username = ? and password = ?";
        JdbcUtil p = new JdbcUtil();
        Connection con = p.getConnection();//调用获取连接方法，得到数据库的连接
        User user = new User();
        try {
            PreparedStatement pst = con.prepareStatement(sql);
            //向sql语句中注入参数
            pst.setObject(1, username);
            pst.setObject(2, password);
            ResultSet rs = pst.executeQuery();//执行sql语句,得到结果集
            if (rs.next()) {//如果查到结果返回true否则返回false
                user.setUsername((String) rs.getObject("username"));//rs.getObject():从查询出的数据中获取指定字段的值
                user.setPassword((String) rs.getObject("password"));
                user.setName((String) rs.getObject("name"));//Ctrl+Shift+Alt+Enter:代码格式化*/
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
    //查询朋友圈数据
    public List<friendc> findByfriend(String username,Integer title){
        String sql=null;
        if (title==0){
            sql = "select u.name,u.imgAddress,f.* from userInfo u,friendc f where u.username=? and f.id !=''";
        }else {
            sql = "select u.name,u.imgAddress,f.* from userInfo u,friendc f where u.username=? and u.username=f.username and f.id !=''";
        }
        JdbcUtil p = new JdbcUtil();
        Connection con = p.getConnection();//调用获取连接方法，得到数据库的连接
        List<friendc> list =new ArrayList<friendc>();
        friendc fr=null;
        try {
            PreparedStatement pst = con.prepareStatement(sql);
            //向sql语句中注入参数
            pst.setObject(1, username);
            ResultSet rs = pst.executeQuery();//执行sql语句,得到结果集
           /* 3。在数据查询出的时候封装，改变格式（1、2分别在实体类和FriendController类）
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//导入时间格式转换器
            String createTime = sdf.format(date);//将数据库查出来的时间戳转换为时间格式，格式为:yyyy-mm-dd*/
            while (rs.next()) {//如果查到结果返回true否则返回false
                fr=new friendc();
                fr.setId((String) rs.getObject("id"));
                fr.setContent((String) rs.getObject("content"));
                fr.setCreatetime((java.util.Date) rs.getObject("createTime"));
                fr.setFimgAddress((String) rs.getObject("fImgAddress"));
                fr.setUsername((String) rs.getObject("username"));
                fr.setImgaddress((String) rs.getObject("imgAddress"));
                fr.setName((String) rs.getObject("name"));
                list.add(fr);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    //根据昵称查询
    public boolean findbyusername(String username) {
        String sql = "select * from userInfo where username = ?";
        JdbcUtil p = new JdbcUtil();
        Connection con = p.getConnection();//调用获取连接方法，得到数据库的连接
        boolean flag = false;
        try {
            PreparedStatement pst = con.prepareStatement(sql);
            //向sql语句中注入参数
            pst.setObject(1, username);
            ResultSet rs = pst.executeQuery();//执行sql语句,得到结果集
            if (rs.next()) {//如果查到结果返回true否则返回false
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    //插入数据
    public boolean Insert(String username, String password, String name) {
        String sql = "insert into userInfo VALUE(?,?,?,null)";
        JdbcUtil p = new JdbcUtil();
        Connection con = p.getConnection();//调用获取连接方法，得到数据库的连接
        boolean flag = false;
        try {
            PreparedStatement pst = con.prepareStatement(sql);
            //向sql语句中注入参数
            pst.setObject(1, username);
            pst.setObject(2, password);
            pst.setObject(3, name);
            int resNum = pst.executeUpdate();
            if (resNum == 1) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    //向数据库插入朋友圈信息
    public void publishFriendC(String username,String content,String fImgAddress) {
        String sql = "insert into friendc value (?,?,?,?,?)";
        String id = UUID.randomUUID().toString();
        JdbcUtil p = new JdbcUtil();
        Connection con = p.getConnection();//调用获取连接方法，得到数据库的连接
        try {
            PreparedStatement pst = con.prepareStatement(sql);
            //向sql语句中注入参数
            pst.setObject(1, id);
            pst.setObject(2, content);
            pst.setObject(3, fImgAddress);
            pst.setObject(4, new Date());
            pst.setObject(5, username);
            pst.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //根据id删除个人朋友圈
    public void delFriendC(String id) {
        String sql = "delete from friendc where id = ?";
        JdbcUtil p=new JdbcUtil();
        Connection con = p.getConnection();//调用获取连接方法，得到数据库的连接
        try {
            PreparedStatement pst = con.prepareStatement(sql);
            //向sql语句中注入参数
            pst.setObject(1, id);
            pst.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
